using System.Windows.Controls;
using aichat.ViewModels;

namespace aichat.Views
{
    public partial class LoginView : UserControl
    {
        public LoginView()
        {
            InitializeComponent();
            
            // 绑定PasswordBox到ViewModel
            this.Loaded += LoginView_Loaded;
        }
        
        private void LoginView_Loaded(object sender, System.Windows.RoutedEventArgs e)
        {
            // 绑定PasswordBox到ViewModel的Password属性
            if (DataContext is LoginViewModel viewModel)
            {
                PasswordBox.PasswordChanged += (s, args) =>
                {
                    viewModel.Password = PasswordBox.Password;
                };
                
                ConfirmPasswordBox.PasswordChanged += (s, args) =>
                {
                    viewModel.ConfirmPassword = ConfirmPasswordBox.Password;
                };
                
                // 监听清空密码框事件
                viewModel.ClearPasswordBoxes += () =>
                {
                    PasswordBox.Password = string.Empty;
                    ConfirmPasswordBox.Password = string.Empty;
                };
                
                // 订阅登录成功事件
                viewModel.LoginSuccessful += (user) =>
                {
                    // 获取MainWindow的DataContext（MainWindowViewModel）
                    var mainWindow = System.Windows.Application.Current.MainWindow;
                    if (mainWindow?.DataContext is MainWindowViewModel mainViewModel)
                    {
                        mainViewModel.OnLoginSuccessful(user);
                    }
                };
            }
        }
    }
} 